package day03;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/7 10:03
 * 力扣 142
 */
public class LinkedListPractice03 {

    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow) {
                // 找到入口
                slow = head;
                while(slow != fast) {
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}
